<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<body>

<script type="text/javascript">
$(function() {
	//alert('ok');
	djt_bs_editIndex = undefined;
	
	var Sobid=<%=request.getAttribute("Sobid")%>;
	//console.log("Sobid"+Sobid);
	
	$('#djt_bs_dg'+Sobid+'').datagrid({
    	url:'../baseSalary/findBSByPage?id='+Sobid+'',
    	columns:[[
    		{field:'',title:'',checkbox:true},
			{field:'baseSalaryId',title:'编号',width:'50',sortable:true},
			{field:'empName',title:'员工名',width:'100'},
			{field:'baseSalaryValue',title:'本薪值',width:'100'},
			{field:'salaryBeginDate',title:'起薪日',width:'100'},
			{field:'salaryOffDate',title:'止薪日',width:'100'},
			{field:'mender',title:'修改者',width:'100'},
			{field:'mendDate',title:'修改日期',width:'100'},
			{field:'approver',title:'批准人',width:'100'},
			{field:'approveDate',title:'批准日期',width:'100'},
			{field:'remark',title:'备注',width:'100',editor:{type:'text'}}
		]],
		fit:true,
 	   	fitColumns:false,
 	   	//隔行变色
 	   	striped:true,
  	  	nowrap:true,
		toolbar:'#bstb'+Sobid+'',
		pagination:true,
		pageSize:5,
		pageList:[5,10,20,40],
		border:false,
		idFieid:'baseSalaryId',
		sortName:'baseSalaryId',
		sortOrder:'asc',
		onAfterEdit : function(rowIndex,rowData,changes){
			var updated = $('#djt_bs_dg'+Sobid+'').datagrid('getChanges','updated');
			var url = '';
			if(updated.length > 0){
				url = '../baseSalary/updateBS';
			}
			var djt_bs_rowData = JSON.stringify(rowData);
			$.post(url,{djt_bs_rowData},function(e){
				var info = JSON.parse(e);
				//console.log(info);
				if(info.success){
					$('#djt_bs_dg'+Sobid+'').datagrid('acceptChanges');
					$.messager.show({
						title:'成功',
						msg:info.msg,
					});
				}else{
					$('#djt_bs_dg'+Sobid+'').datagrid('rejectChanges');
					$.messager.alert('错误',info.msg,'error');
				}
			})
			djt_bs_editIndex = undefined;
			$('#djt_bs_dg'+Sobid+'').datagrid('unselectAll');
		},
		onDblClickRow : function(rowIndex,rowData){
			if(djt_bs_editIndex != undefined){
				$('#djt_bs_dg'+Sobid+'').datagrid('endEdit',djt_bs_editIndex);
				djt_bs_editIndex = undefined;
			}
			if(djt_bs_editIndex == undefined){
				$('#djt_bs_dg'+Sobid+'').datagrid('beginEdit',rowIndex);
				djt_bs_editIndex = rowIndex;
			}
		}
	});
});

//保存
function savebs(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	$('#djt_bs_dg'+Sobid+'').datagrid('endEdit',djt_bs_editIndex);
}

//删除
function removebs(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	var rows = $('#djt_bs_dg'+Sobid+'').datagrid('getSelections');
	if(rows.length > 0){
		$.messager.confirm('请确认','您确定要删除当前所有选中的项目吗？',function(r){
			if(r){
				var ids = [];
				for(var i = 0; i < rows.length; i++){
					ids.push(rows[i].baseSalaryId)
				}
				var djt_bs_rowData = JSON.stringify(ids);
				$.post('../baseSalary/deleteSob',{djt_bs_rowData},function(e){
					var info = JSON.parse(e);
					if(info.success){
						$('#djt_bs_dg'+Sobid+'').datagrid('load');
						$('#djt_bs_dg'+Sobid+'').datagrid('unselectAll');
						$.messager.show({
							title:'成功',
							msg:info.msg,
						});
					}else{
						$('#djt_bs_dg'+Sobid+'').datagrid('rejectChanges');
						$.messager.alert('错误',info.msg,'error');
					}
				})
				djt_bs_editIndex = undefined;
			}
		})
	}else{
		$.messager.alert('提示','请选择要删除的记录！','error');
	}
}

//编辑
function editbs(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	var rows = $('#djt_bs_dg'+Sobid+'').datagrid('getSelections');
	if(rows.length == 1){
		if(djt_bs_editIndex != undefined){
			$('#djt_bs_dg'+Sobid+'').datagrid('endEdit',djt_bs_editIndex);
		}
		if(djt_bs_editIndex == undefined){
			var index = $('#djt_bs_dg'+Sobid+'').datagrid('getRowIndex',rows[0]);
			$('#djt_bs_dg'+Sobid+'').datagrid('beginEdit',index);
			djt_bs_editIndex = index;
			$('#djt_bs_dg'+Sobid+'').datagrid('unselectAll');
		}
	}else{
		$.messager.show({
			title:'提示',
			msg:'一次只能编辑一行！',
		});
	}
}

//撤销编辑
function rejectbs(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	djt_bs_editIndex = undefined;
	$('#djt_bs_dg'+Sobid+'').datagrid('rejectChanges');
	$('#djt_bs_dg'+Sobid+'').datagrid('unselectAll');
} 
</script>
<div class="easyui-layout" fit="true" border="false">
	<div region="center" border="false">
		<table id="djt_bs_dg${Sobid}"></table>
	
		<div id="bstb${Sobid}">
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removebs()">删除</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editbs()">编辑</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="savebs()">保存</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="rejectbs()">撤销编辑</a>
		</div>
	</div>	
</div>
</body>